home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue99 / CPROG3.CPP next >
Encoding:
C/C++ Source or Header  |  1994-10-05  |  1.4 KB  |  50 lines

  1. /* CPROG3.CPP - some examples of arithmetic on an integer variable */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5.  
  6. void pause(void)
  7. {
  8.     printf("\nPress a key...\n");
  9.     while( kbhit() == 0)
  10.         ;
  11.     getch();
  12. }
  13.  
  14.  
  15. main()
  16. {
  17. int i;                                  // Create local integer variable i
  18.     clrscr();            // Clear the screen
  19.     i=6;                // Set i to value 6
  20.     printf("i starts out as %d\n", i); // Print its value
  21.                 /* The %d in the string tells printf() to
  22.                 print a numeric value at that point. It
  23.                 expects to find it as a second parameter,
  24.                 the two parameters inside the brackets
  25.                 being separated by a comma. The d part of
  26.                 %d specifies that the number is to be
  27.                 shown as a decimal value. */
  28.     i = i + 4;            // Add 4 to i...
  29.     printf("i+4=%d\n", i);        // ...and print the new value
  30.     i++;                // Add 1 to i. i++ is a short-
  31.                     // hand way of saying i=i+1;
  32.     printf("i++=%d\n", i);
  33.     i *= 7;                // i *= 7 is shorthand for i=i*7;
  34.                     // ...which would work equally as
  35.                     // well.
  36.     printf("i*=7=%d\n",i);
  37.     printf("i+=4=%d\n",i+=4);    // Add 4 again. Note how i+=4 is
  38.                     // evaluated before being passed
  39.                     // through to %d
  40.  
  41.     i -= 4;                // Subtraction: alternatively i=i-4;
  42.     i /= 7;                // Division: i=i/7; also works.
  43.                     // Other operators are available
  44.                     // such as modulo (%) - read about
  45.                     // them in the online help under
  46.                     // operators.
  47.  
  48.     pause();            // Wait for a keypress
  49. }
  50.